<?php
namespace Modules\Goods\Http\Controllers\Admin;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Modules\Goods\Entities\Category;
use Encore\Admin\Controllers\HasResourceActions;
use Modules\Goods\Http\Controllers\Admin\CategoryService;

class CategoryController extends Controller
{
    use HasResourceActions;

    public function __construct(CategoryService $sCategory)
    {
        $this->sCategory = $sCategory;
    }

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content, CategoryService $sCategory)
    {
        return $content
            ->header('商品分类')
            ->description('description')
            ->row(function (Row $row) {
                $row->column(6, Category::tree(function ($tree) {
                    // 查询商户自己的商品分类
                    $tree->query($this->sCategory->selectOptions());

                    $tree->disableCreate();
                }));

                //                $row->column(6, $this->form());
                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->text('title', '名称');
                    $form->select('parent_id', '父级分类')->options(Category::selectOptions($this->sCategory->selectOptions()));

                    $column->append((new Box(trans('admin.new'), $form))->style('default'));
                });
            });
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Category);



        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Category::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Category);

        $form->display('id', 'ID');

        $form->text('title', '名称');
        $form->select('parent_id', '父级分类')->options(Category::selectOptions());

        $form->display('created_at', 'Created At');
        $form->display('updated_at', 'Updated At');

        return $form;
    }
}
